package wuziqi;
import java.util.Scanner;

// 游戏核心引擎
class BoardGameEngine {
    private GameConfig config;
    private GameState state;
    private WinChecker winChecker;
    private AI computerAI;
    private Scanner scanner;
    private boolean againstComputer;
    
    public BoardGameEngine(GameConfig config) {
        this.config = config;
        this.state = new GameState(config.getBoardSize(), config.getEmptySymbol());
        this.winChecker = new ContinuousWinChecker();
        this.scanner = new Scanner(System.in);
    }
    
    public void printBoard() {
        int size = config.getBoardSize();
        char[][] board = state.getBoard();
        
        // 打印列号
        System.out.print("   ");
        for (int j = 0; j < size; j++) {
            System.out.printf("%2d ", j + 1);
        }
        System.out.println();
        
        // 打印棋盘
        for (int i = 0; i < size; i++) {
            System.out.printf("%2d ", i + 1);
            for (int j = 0; j < size; j++) {
                System.out.print(" " + board[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    public boolean makeMove(int row, int col) {
        int size = config.getBoardSize();
        char[][] board = state.getBoard();
        
        if (row < 0 || row >= size || col < 0 || col >= size) {
            System.out.println("坐标不合法");
            System.out.println("请重新输入:");
            return false;
        }
        
        if (board[row][col] != config.getEmptySymbol()) {
            System.out.println("该坐标已经被占用");
            System.out.println("请重新输入:");
            return false;
        }
        
        board[row][col] = state.getCurrentPlayer();
        state.setLastMove(row,col);  // 记录最后移动的坐标
        state.incrementMoveCount();
        return true;
    }
    
    public void chooseGameMode() {
        System.out.println("选择游戏模式:");
        System.out.println("1. 玩家对战");
        System.out.println("2. 对战电脑");
        System.out.print("请输入选择 (1 或 2): ");
        
        int choice = scanner.nextInt();
        againstComputer = (choice == 2);
    }
    
    public void playerMove() {
        int size = config.getBoardSize();
        int row, col;
        
        do {
            System.out.println("玩家 " + state.getCurrentPlayer() + "，请输入行和列 (1-" + size + "):");
            System.out.print("行: ");
            row = scanner.nextInt() - 1;
            System.out.print("列: ");
            col = scanner.nextInt() - 1;
        } while (!makeMove(row, col));
    }
    
    public void computerMove() {
        int[] move = computerAI.makeMove(state, config);
        if (move != null) {
            makeMove(move[0], move[1]);
            System.out.println("电脑在位置 (" + (move[0] + 1) + ", " + (move[1] + 1) + ") 放置了 " + state.getCurrentPlayer());
        }

    }
    
    public void switchPlayer() {
        char current = state.getCurrentPlayer();
        state.setCurrentPlayer(current == config.getPlayer1Symbol() ? 
                              config.getPlayer2Symbol() : config.getPlayer1Symbol());
    }
    
    public boolean isBoardFull() {
        int size = config.getBoardSize();
        char[][] board = state.getBoard();
        char empty = config.getEmptySymbol();
        
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (board[i][j] == empty) {
                    return false;
                }
            }
        }
        return true;
    }

    public void chooseAI() {
    System.out.println("选择AI难度:");
    System.out.println("1. 简单 (随机)");
    System.out.println("2. 中等 (基于评分)");
    System.out.println("3. 困难 (极小化极大算法)");
    System.out.println("4. 专家 (通用专业AI)");
    System.out.print("请输入选择 (1-4): ");
    
    int choice = scanner.nextInt();
    switch (choice) {
        case 1:
            this.computerAI = new RandomAI();
            break;
        case 2:
            this.computerAI = new ScoreBasedAI();
            break;
        case 3:
            this.computerAI = new MinimaxAI();
            break;
        case 4:
            this.computerAI = new UniversalProfessionalAI();
        default:
            this.computerAI = new UniversalProfessionalAI();
    }
}
    public void play() {
        System.out.println("欢迎来到棋盘游戏!");
        System.out.println("棋盘大小: " + config.getBoardSize() + "x" + config.getBoardSize());
        System.out.println("获胜条件: 连续" + config.getWinCount() + "个棋子");
        chooseGameMode();
        if(againstComputer)
            chooseAI();  // 选择不同难度的AI
        boolean gameWon = false;
        
        while (!gameWon && !isBoardFull()) {
            printBoard();
            
            if (againstComputer && state.getCurrentPlayer() == config.getPlayer2Symbol())
                computerMove(); 
            else
                playerMove();
            
            
            if (winChecker.checkWin(state, config)) {
                printBoard();
                System.out.println("恭喜! 玩家 " + state.getCurrentPlayer() + " 获胜!");
                gameWon = true;
            } else if (isBoardFull()) {
                printBoard();
                System.out.println("平局!");
            } else {
                switchPlayer();
            }
        }
    }
}
